package com.kfm.base.io.xml;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.URLEncoder;

public class Demo11 {

    public static void main(String[] args) {
        String song = URLEncoder.encode("2002年的第一场雪");

        final String pre_path = "https://www.xzmp3.com/";

        HttpResponse response = HttpRequest.get(pre_path + "xiazai/" + song + ".htm")
                .execute();

        String string = response.toString();

        Document parse = Jsoup.parse(string);

        Elements returnList = parse.getElementsByClass("return_list");

        // 歌曲列表
        Element list = returnList.get(0);

//        List<Node> nodes = list.childNodes();
        Elements children = list.children();

        for (Element element : children){ // return_item
            Element handle = element.getElementsByClass("handle").get(0);
            String attr = handle.attr("href");

            Element name = element.getElementsByClass("name").get(0);

            System.out.println("0. " + name.text());
//            System.out.println(pre_path + attr + " = " + name.text());
//            System.out.println("准备下载 " + name);
//            downloadMusic(pre_path + attr, name.text());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

//        Element handle = children.get(0).getElementsByClass("handle").get(0);
//        String attr = handle.attr("href");
//
//        Element name = children.get(0).getElementsByClass("name").get(0);
////            System.out.println(pre_path + attr + " = " + name.text());
//        System.out.println("准备下载 " + name);
//        downloadMusic(pre_path + attr, name.text());
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

    }

    public static void downloadMusic(String path, String name) {
        HttpResponse response = HttpRequest.get(path).execute();

        String html = response.toString();

        Document parse = Jsoup.parse(html);

        Element element = parse.getElementsByClass("bt_download").get(0);
        String url = element.attr("data-url");

        HttpResponse musicResp = HttpRequest.get(url).execute();

        // 两次重定向
        String location = musicResp.header("location");

        String location1 = HttpRequest.get(location).execute().header("location");

        InputStream inputStream = HttpRequest.get(location1).execute().bodyStream();

//        int status = musicResp.getStatus();
//        InputStream inputStream = musicResp.bodyStream();
//

        System.out.println("正在下载" + name);
        try {
            if (inputStream.available() != 0) { // 重定向

                File file = new File("d:/io/music/", name + ".mp3");
                if (!file.getParentFile().exists()){
                    file.getParentFile().mkdirs();
                }
                try (FileOutputStream fos = new FileOutputStream(file);){
                    fos.write(inputStream.readAllBytes());
                    System.out.println(name + "下载成功");
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                System.out.println(name + " 没有版权 ");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
